<?php

namespace App\Http\Requests;

use App\Http\Requests\BaseFormRequest;

class DeviceinfoRequest extends BaseFormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function scenes()
    {
        return [
            'store'=>[
				'pdi_name' => 'required|max:64',
				'pdi_code' => 'required|max:255',
				'dpt_id' => 'required|integer|exists:t_devicetype,dt_typeid',
				'AreaId' => 'required|integer',
			],
			'update'=>[
				'pdi_name' => 'sometimes|required|max:64',
				// 'pdi_code' => 'required|max:255|unique:t_prideviceinfo,pdi_code',
				'pdi_code' => 'sometimes|required|max:255',
				'dpt_id' => 'sometimes|required|integer|exists:t_devicetype,dt_typeid',
				'AreaId' => 'sometimes|required|integer',
			]
        ];
    }
	
	public function messages()
    {
        return [
			'pdi_name.required' => '设备名称必须',
			'pdi_name.max' => '设备名称不能超过64个字符',
			'pdi_code.required' => '设备编码必须',
			'pdi_code.max' => '设备编码不能超过255个字符',
			'pdi_code.unique' => '设备编码已存在',
			'dpt_id.required' => '设备分类编号必须',
			'dpt_id.integer' => '设备分类编号必须是数字',
			'dpt_id.exists' => '设备分类不存在',
			'AreaId.required' => '区域必须',
			'AreaId.integer' => '区域必须是数字',
			'Co_ID.required' => '公司必须',
			'Co_ID.integer' => '公司必须是数字',
			'Co_ID.exists' => '公司不存在',
        ];
    }
	
	public function withValidator($validator)
	{
		$validator->sometimes('Co_ID', 'required|integer|exists:t_companycnfo,Co_ID', function($input){
			$user = $input->user();
            return $user->isSuper();
        });
	}
}
